// pages/integralRecharge/integralRecharge.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    integral:[
      {num:100,checked:false,price:1},
      { num: 600, checked: false, price: 5 },
      { num: 1500, checked: false, price: 10}
    ],
    canSubmit:false,
    currentData:{},
    userInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
  },
  choiseIntegral:function(e){//选择积分事件
    var index=e.currentTarget.dataset.index;
    this.data.integral.forEach(function(ele){
      ele.checked=false;
    })
    this.data.integral[index].checked=true;
    this.setData({
      integral: this.data.integral,
      canSubmit:true,
      currentData: this.data.integral[index]
    })
  },
  recharge:function(e){//立即充值
    var that=this;
    if (this.data.canSubmit){
      wx.request({
        url: app.globalData.httpApiCar + 'integralInfo/replenishIntegration', //仅为示例，并非真实的接口地址
        data: {
          openId: app.globalData.openId,
          userId:this.data.userInfo.id,
          money: this.data.currentData.price
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            that.requestPayment(res.data.data);
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.error,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                }
              }
            })
          }
        }
      })
    }
  },
  requestPayment: function (obj) {
    var that = this;
    wx.requestPayment({
      'timeStamp': obj.timeStamp,
      'nonceStr': obj.nonceStr,
      'package': obj.package,
      'signType': obj.signType,
      'paySign': obj.paySign,
      'success': function (res) {
        wx.setStorage({
          key: 'url',
          data: '/pages/myPoints/myPoints'
        })
        wx.redirectTo({
          url: '/pages/regsuccess/regsuccess?showText=充值成功&btnText=我的积分'
        })
      },
      'fail': function (res) {
        wx.showModal({
          title: '提示',
          content: '支付失败',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})